<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<!--查询部门表单-->
<form class="layui-form" action="" lay-filter="departmentForm">
    <div class="layui-form-item" style="margin-top:10px" >
        <div class="layui-inline">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-inline">
                <select name="bumen" lay-verify="required" lay-search>
                    <option value="0">直接</option>
                    <option value="1">北京</option>
                    <option value="2">上海</option>
                    <option value="3">广州</option>
                    <option value="4">深圳</option>
                    <option value="5">杭州</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">员工名称</label>
            <div class="layui-input-inline">
                <select name="yuangong" lay-verify="required" lay-search>
                    <option value="0">直接选择</option>
                    <option value="1">北京</option>
                    <option value="2">上海</option>
                    <option value="3">广州</option>
                    <option value="4">深圳</option>
                    <option value="5">杭州</option>
                </select>
            </div>
        </div>
        <button type="button" id="formsubmit" class="layui-btn" lay-submit="" lay-filter="demo1"><i class="layui-icon layui-icon-search"></i>查询</button>
        <span id="add" class="layui-btn" lay-submit="" lay-filter="pageSubmit"><i class="layui-icon layui-icon-user"></i>添加客户</span>
        <button type="button" id="delete" class="layui-btn" lay-submit="" lay-filter="demo1"><i class="layui-icon layui-icon-fonts-del"></i>已删除客户</button>
    </div>
</form>
<!--部门表单-->
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">

<a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-search"></i>查询</a>
</script>


<script src="../../layui/layui.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table','form','layer'], function(){
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;

        table.render({
            id : 'departmentTable'
            ,elem: '#test'
            ,url:'/firstWeb/layuidepartment'
            ,title: '用户数据表'
            ,where: {}
            ,cols: [[
                {field:'p_id', title:'部门编号'}
                ,{field:'p_name', title:'部门名称'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
            ]]
            ,page: true // 默认一页十条
            ,limit:5
            ,limits: [1,5,10,20]
        });
        function tableReload() {

            var data = form.val('departmentForm');
            table.reload('departmentTable',{
                where : data
                ,page: {
                    curr: 1
                }
            })
        }

        $("#formsubmit").click(function(){
            tableReload();
        })
        $("#add").click(function(){

            layer.open({
                type: 2 //此处以iframe举例
                ,title: '添加客户'
                ,area: ['450px', '450px']
                ,shade: 0
                ,maxmin: true
                ,content: 'addkehu.html'
                ,btn: ['添加客户', '关闭']
                ,yes: function(index){
                    var data = window['layui-layer-iframe' + index].getValue();
                    if (!data.p_name) {
                        layer.msg("客户名称不能为空");
                        return false;
                    }

                    //发送新增请求 get/post √
                    axios.post('/firstWeb/layuidepartment', data).then(function (response) {
                        //post请求执行成功的回调
                        if (response.data.status == "0") {
                            layer.close(index);
                            //刷新表格
                            tableReload();
                            layer.msg("新增客户成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                }
                ,btn2: function(index){//5
                    layer.close(index);
                }
            });
        })

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index) {
                    axios.post('/firstWeb/updatePart', data).then(function (response) {
                        //post请求执行成功的回调
                        if (response.data.status == "1") {
                            layer.close(index);
                            //刷新表格
                            tableReload();
                            layer.msg("注销成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                })
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2 //此处以iframe举例
                    ,title: ''
                    ,area: ['390px', '340px']
                    ,shade: 0
                    ,maxmin: true
                    ,content: 'updatePart.html'
                    ,btn: ['添加客户', '关闭']
                    ,yes: function(index){
                        data = window['layui-layer-iframe' + index].getValue();
                        data.p_id = obj.data.p_id
                        if (!data.p_name) {
                            layer.msg("客户名称不能为空");
                            return false;
                        }

                        //发送新增请求 get/post √
                        axios.post('/firstWeb/update', data).then(function (response) {
                            //post请求执行成功的回调
                            if (response.data.status == "2") {
                                layer.close(index);
                                //刷新表格
                                tableReload();
                                layer.msg("修改成功");
                            } else {
                                layer.msg(response.data.msg)
                            }
                        }).catch(function (error) {
                            console.log(error);
                        });
                    },
                    success:function(layero ,index){
                        <!--给弹出表单赋值-->
                        var div = layero.find('iframe').contents().find('#part');
                        div.find("#p_id").val(data.p_id);
                        div.find("#p_name").val(data.p_name);
                        div.find("#p_is").val(data.p_is);
                    }
                    ,btn2: function(index){//5
                        layer.close(index);
                    }
                });
            }
        });
    });
</script>


</body>
</html>

